<?php

/*
 *    This file is part of the module OxProbs for OXID eShop Community Edition.
 *
 *    The module OxProbs for OXID eShop Community Edition is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    The module OxProbs for OXID eShop Community Edition is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with OXID eShop Community Edition.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @link   https://code.google.com/p/oxprobs/
 */
 
class oxprobs_reports extends oxprobs_reports_parent
{
    protected $_sThisTemplate = "oxprobs/oxprobs_reports.tpl";
    public function render()
    {
        parent::render();
        $oSmarty = oxUtilsView::getInstance()->getSmarty();
        $oSmarty->assign( "oViewConf", $this->_aViewData["oViewConf"]);
        $oSmarty->assign( "shop", $this->_aViewData["shop"]);

        $cReportType = isset($_POST['oxprobs_reporttype']) ? $_POST['oxprobs_reporttype'] : $_GET['oxprobs_reporttype']; 
        if (empty($cReportType))
            $cReportType = "nostock";
        $oSmarty->assign( "ReportType", $cReportType );
        
        switch ($cReportType) {
            case 'nostock':
                $sSql1 = 'SELECT oxid, oxartnum, oxean, oxmpn, oxtitle, oxvarselect, oxstock, oxprice '
                        . 'FROM oxarticles '
                        . 'WHERE oxstock <= 0 '
                        . 'AND oxactive = 1 '
                        . 'AND oxstockflag = 1 '
                        . 'AND oxvarcount = 0 '
                        . 'AND oxparentid = \'\' '
                        . 'ORDER BY oxtitle ';
                $sSql2 = 'SELECT a.oxid AS oxid, a.oxparentid AS oxparentid, a.oxartnum AS oxartnum, a.oxean AS oxean, a.oxmpn AS oxmnp, '
                        . '(SELECT b.oxtitle FROM oxarticles b WHERE a.oxparentid = b.oxid) AS oxtitle, '
                        . 'a.oxvarselect AS oxvarselect, a.oxstock AS oxstock, a.oxprice AS oxprice '
                        . 'FROM oxarticles a '
                        . 'WHERE a.oxstock <= 0 '
                        . 'AND a.oxactive = 1 '
                        . 'AND a.oxstockflag = 1 '
                        . 'AND a.oxparentid != \'\' '
                        . 'ORDER BY oxtitle ';
                //echo $sSql2;
                //$sSql2 = '';
                break;

            case 'noprice':
                $sSql1 = 'SELECT oxid, oxartnum, oxean, oxmpn, oxtitle, oxvarselect, oxstock, oxprice '
                        . 'FROM oxarticles '
                        . 'WHERE oxprice <= 0.01 '
                        . 'AND oxactive = 1 '
                        . 'AND oxvarcount = 0 '
                        . 'AND oxparentid = \'\' '
                        . 'ORDER BY oxtitle ';
                $sSql2 = 'SELECT a.oxid AS oxid, a.oxparentid AS oxparentid, a.oxartnum AS oxartnum, '
                        . '(SELECT b.oxtitle '
                            . 'FROM oxarticles b '
                            . 'WHERE a.oxparentid = b.oxid) '
                            . 'AS oxtitle, '
                        . 'a.oxvarselect AS oxvarselect, a.oxstock AS oxstock, a.oxprice AS oxprice '
                        . 'FROM oxarticles a '
                        . 'WHERE a.oxprice <= 0.01 '
                        . 'AND a.oxactive = 1 '
                        . 'AND a.oxparentid != \'\' '
                        . 'ORDER BY oxtitle ';
                break;

            case 'noean':
                $sSql1 = 'SELECT oxid, oxartnum, oxean, oxmpn, oxtitle, oxvarselect, oxstock, oxprice '
                        . 'FROM oxarticles '
                        . 'WHERE oxean = \'\' '
                        . 'AND oxactive = 1 '
                        . 'AND oxvarcount = 0 '
                        . 'AND oxparentid = \'\' '
                        . 'ORDER BY oxtitle ';
                $sSql2 = 'SELECT  a.oxid AS oxid, a.oxparentid AS oxparentid, a.oxartnum, (SELECT b.oxtitle FROM oxarticles b WHERE a.oxparentid = b.oxid) as oxtitle, a.oxvarselect, a.oxstock, ' 
                        . 'a.oxstocktext, a.oxnostocktext, a.oxstockflag, a.oxdelivery, a.oxremindamount, a.oxvarcount, oxstock, oxean '
                        . 'FROM oxarticles a '
                        . 'WHERE a.oxean = \'\' '
                        . 'AND a.oxactive = 1 '
                        . 'AND a.oxparentid != \'\' '
                        . 'ORDER BY oxtitle ';
                //$sSql2 = '';
                break;

            case 'nompn':
                $sSql1 = 'SELECT oxid, oxartnum, oxean, oxmpn, oxtitle, oxvarselect, oxstock, oxprice '
                        . 'FROM oxarticles '
                        . 'WHERE oxmpn = \'\' '
                        . 'AND oxactive = 1 '
                        . 'AND oxvarcount = 0 '
                        . 'AND oxparentid = \'\' '
                        . 'ORDER BY oxtitle ';
                $sSql2 = 'SELECT  a.oxid AS oxid, a.oxparentid AS oxparentid, a.oxartnum, (SELECT b.oxtitle FROM oxarticles b WHERE a.oxparentid = b.oxid) as oxtitle, a.oxvarselect, a.oxstock, ' 
                        . 'a.oxstocktext, a.oxnostocktext, a.oxstockflag, a.oxdelivery, a.oxremindamount, a.oxvarcount, oxstock, oxean '
                        . 'FROM oxarticles a '
                        . 'WHERE a.oxmpn = \'\' '
                        . 'AND a.oxactive = 1 '
                        . 'AND a.oxparentid != \'\' '
                        . 'ORDER BY oxtitle ';
                //$sSql2 = '';
                break;

            case 'nocat':
                $sSql1 = 'SELECT a.oxid AS oxid, a.oxartnum AS oxartnum, a.oxtitle AS oxtitle, a.oxvarselect AS oxvarselect, '
                        . 'a.oxstock AS oxstock, a.oxprice AS oxprice '
                        . 'FROM oxarticles a '
                        . 'WHERE NOT EXISTS ( '
                            . 'SELECT * '
                            . 'FROM oxobject2category o2c '
                            . 'WHERE a.oxid = o2c.oxobjectid '
                            . ') '
                        . 'AND oxactive = 1 '
                        . 'AND a.oxparentid = \'\' '
                        . 'ORDER BY a.oxtitle ';
                $sSQL2 = '';
                break;

            case 'orphan':
                $sSql1 = 'SELECT a.oxid AS oxid, a.oxparentid AS oxparentid, a.oxartnum AS oxartnum, '
                        . '(SELECT b.oxtitle '
                            . 'FROM oxarticles b '
                            . 'WHERE a.oxparentid = b.oxid) AS oxtitle, '
                        . 'a.oxvarselect AS oxvarselect, a.oxstock AS oxstock, a.oxprice AS oxprice '
                        . 'FROM oxarticles a '
                        . 'WHERE a.oxactive = 1 '
                        . 'AND (SELECT b.oxactive '
                            . 'FROM oxarticles b '
                            . 'WHERE a.oxparentid = b.oxid) = 0 '
                        . 'AND a.oxparentid != \'\' '
                        . 'ORDER BY a.oxtitle ';
                $sSQL2 = '';
                break;

            case 'nodesc':
                $sSql1 = 'SELECT a.oxid AS oxid, a.oxartnum AS oxartnum, a.oxtitle AS oxtitle, a.oxvarselect AS oxvarselect, '
                        . 'a.oxstock AS oxstock, a.oxprice AS oxprice, e.oxlongdesc  '
                        . 'FROM oxarticles a, oxartextends e  '
                        . 'WHERE a.oxid = e.oxid '
                        . 'AND TRIM(e.oxlongdesc) = \'\' '
                        . 'AND oxactive = 1 '
                        . 'AND a.oxparentid = \'\' '
                        . 'ORDER BY a.oxtitle ';
                $sSql2 = 'SELECT a.oxid AS oxid, a.oxartnum AS oxartnum, '
                        . '(SELECT b.oxtitle FROM oxarticles b WHERE a.oxparentid = b.oxid) AS oxtitle, '
                        . 'a.oxvarselect AS oxvarselect, a.oxstock AS oxstock, a.oxprice AS oxprice, e.oxlongdesc '
                        . 'FROM oxarticles a, oxartextends e '
                        . 'WHERE a.oxid = e.oxid '
                        . 'AND TRIM(e.oxlongdesc) = \'\' '
                        . 'AND (SELECT TRIM(f.oxlongdesc) FROM oxarticles b, oxartextends f WHERE b.oxid = f.oxid AND b.oxid = a.oxparentid) = \'\' '
                        . 'AND oxactive = 1 '
                        . 'AND a.oxparentid != \'\' '
                        . 'ORDER BY a.oxtitle ';
                break;

            case 'nomanu':
                $sSql1 = 'SELECT a.oxid AS oxid, a.oxartnum AS oxartnum, a.oxtitle AS oxtitle, a.oxvarselect AS oxvarselect, '
                        . 'a.oxstock AS oxstock, a.oxprice AS oxprice, a.oxmanufacturerid  '
                        . 'FROM oxarticles a '
                        . 'WHERE a.oxmanufacturerid = \'\' '
                        . 'AND oxactive = 1 '
                        . 'AND a.oxparentid = \'\' '
                        . 'ORDER BY a.oxtitle ';
                $sSql2 = '';
                break;

            case 'novend':
                $sSql1 = 'SELECT a.oxid AS oxid, a.oxartnum AS oxartnum, a.oxtitle AS oxtitle, a.oxvarselect AS oxvarselect, '
                        . 'a.oxstock AS oxstock, a.oxprice AS oxprice, a.oxmanufacturerid  '
                        . 'FROM oxarticles a '
                        . 'WHERE a.oxvendorid = \'\' '
                        . 'AND oxactive = 1 '
                        . 'AND a.oxparentid = \'\' '
                        . 'ORDER BY a.oxtitle ';
                $sSql2 = '';
                break;
        }

        //$oArticle = oxNew( "oxarticle" );
        //$oArticle->load( $oxid );

        $i = 0;
        //$aArticles = array();
        $xArticles = array();

        $rs = oxDb::getDb(true)->Execute($sSql1);
        /*echo '<pre>';
        echo $sSql1;
        echo '</pre>';*/
        while (!$rs->EOF) {
            //$oArticle = oxNew( "oxarticle" );
            //$oArticle->load($rs->fields['oxid']);
            /*echo '<pre>';
                print_r($oArticle);
            echo '</pre><hr>';*/

            /*echo '<pre>';
            print_r($rs);
            echo '</pre>';*/
            //$aArticles[$i] = $oArticle;
            //$i++;
            //array_push($aArticles,$oArticle);
            array_push($xArticles, $rs->fields);
            $rs->MoveNext();
        }
        
        if (!empty($sSql2)) {
            $rs = oxDb::getDb(true)->Execute( $sSql2);
            while (!$rs->EOF) {
                //$oArticle = oxNew( "oxarticle" );
                //$oArticle->load($rs->fields['oxid']);
                //$oParentArticle = oxNew( "oxarticle" );
                //$oParentArticle->load($rs->fields['oxparentid']);
                //$oArticle->oxarticles__oxtitle = $oParentArticle->oxarticles__oxtitle;
                /*echo '<pre>';
                    print_r($oArticle);
                echo '</pre><hr>';*/

                /*echo '<pre>';
                print_r($rs);
                echo '</pre>';*/
                //$aArticles[$i] = $oArticle;
                //$i++;
                //array_push($aArticles,$oArticle);
                array_push($xArticles, $rs->fields);
                $rs->MoveNext();
            }
        }

         /*echo '<pre>';
         print_r($xArticles);
         echo '</pre>';*/
         //-$this->_aViewData["aArticles"] = $aArticles;
         //$oSmarty->assign("aArticles", $aArticles);
         //-$oSmarty->assign("aArticles", $this->_aViewData["aArticles"]);
         $oSmarty->assign("xArticles",$xArticles);

         //echo $oSmarty->fetch("oxprobs/oxprobs_reports.tpl");
         //oxUtils::getInstance()->showMessageAndExit( "" );
         
         return $this->_sThisTemplate;
   }
 }
?>